﻿namespace BloggerBackup
{
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;

    [Serializable]
    public class SelectedSaveNameOptions
    {
        private List<NameOption> _folderNameOptions = new List<NameOption>();
        private List<NameOption> _postNameOptions = new List<NameOption>();

        public string GetPath(string blogTitle, string PrimaryBackupPath, Stream atomXMLStream)
        {
            string str2 = PrimaryBackupPath;
            XmlDocument document = new XmlDocument();
            atomXMLStream.Position = 0L;
            document.Load(atomXMLStream);
            foreach (NameOption option in this._folderNameOptions)
            {
                string str3 = "";
                if (option.BaselineXMLTag.ToUpper() == "BLOGTITLE")
                {
                    str3 = blogTitle;
                }
                else
                {
                    str3 = Conversions.ToDate(document.GetElementsByTagName(option.BaselineXMLTag).Item(0).InnerText).ToString(option.FormatString);
                }
                if (str3.Length > 0)
                {
                    str2 = Path.Combine(str2, this.PrepareValueforFileSystem(str3));
                }
            }
            return str2;
        }

        public string GetPostFileName(Stream atomXMLStream)
        {
            XmlDocument document = new XmlDocument();
            atomXMLStream.Position = 0L;
            document.Load(atomXMLStream);
            string str2 = "";
            foreach (NameOption option in this._postNameOptions)
            {
                IEnumerator enumerator;
                string innerText = "";
                if (option.BaselineXMLTag.ToUpper() == "TITLE")
                {
                    if (document.GetElementsByTagName("title").Count > 0)
                    {
                        innerText = document.GetElementsByTagName("title").Item(0).InnerText;
                    }
                    else
                    {
                        innerText = "NoTitle-" + Conversions.ToDate(document.GetElementsByTagName("published").Item(0).InnerText).ToString("yyyyMMddhhmmss");
                    }
                    goto Label_0253;
                }
                if (option.BaselineXMLTag.ToUpper() == "CUSTOMTEXT")
                {
                    innerText = option.CustomText;
                    goto Label_0253;
                }
                if (option.BaselineXMLTag.ToUpper() != "LINKALTERNATE")
                {
                    goto Label_0225;
                }
                XmlNodeList elementsByTagName = document.GetElementsByTagName("link");
                string str4 = "";
                try
                {
                    enumerator = elementsByTagName.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        XmlNode current = (XmlNode) enumerator.Current;
                        if (current.Attributes["rel"].InnerText == "alternate")
                        {
                            str4 = current.Attributes["href"].InnerText;
                            goto Label_01C3;
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            Label_01C3:;
                if ((((str4.Length > 0) && (str4.LastIndexOf("/") > -1)) ? 1 : 0) != 0)
                {
                    innerText = str4.Substring(str4.LastIndexOf("/") + 1, (str4.Length - str4.LastIndexOf("/")) - 1).Replace(".html", "");
                }
                goto Label_0253;
            Label_0225:
                innerText = Conversions.ToDate(document.GetElementsByTagName(option.BaselineXMLTag).Item(0).InnerText).ToString(option.FormatString);
            Label_0253:
                if (innerText.Length > 0)
                {
                    str2 = str2 + this.PrepareValueforFileSystem(innerText);
                }
            }
            return str2;
        }

        private string PrepareValueforFileSystem(string Value)
        {
            char[] invalidPathChars = Path.GetInvalidPathChars();
            string str2 = Value;
            foreach (char ch in invalidPathChars)
            {
                str2 = str2.Replace(Conversions.ToString(ch), "_");
            }
            foreach (char ch2 in Path.GetInvalidFileNameChars())
            {
                str2 = str2.Replace(Conversions.ToString(ch2), "_");
            }
            return str2;
        }

        public List<NameOption> FolderNameOptions
        {
            get
            {
                return this._folderNameOptions;
            }
            set
            {
                this._folderNameOptions = value;
            }
        }

        public List<NameOption> PostNameOptions
        {
            get
            {
                return this._postNameOptions;
            }
            set
            {
                this._postNameOptions = value;
            }
        }
    }
}

